package com.cisdi.info.simple.authenticate;

import com.cisdi.info.simple.dto.operator.LoginDTO;
import com.cisdi.info.simple.entity.permission.ModuleTreeNode;
import com.cisdi.info.simple.service.base.BaseService;
import com.cisdi.info.simple.service.permission.ModuleService;
import com.cisdi.info.simple.service.permission.OperatorService;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@RestController
@RequestMapping("/Login")
@CrossOrigin(allowCredentials = "true")
public class LoginInfoController {

    @Autowired
    private ModuleService moduleService;

    @Autowired
    private OperatorService operatorService;

    @Autowired
    private BaseService baseService;

    @PostMapping("/loginSubmit")
    public Map<String,Object> loginSubmit(@Valid @RequestBody LoginDTO loginDTO) throws JSONException {

        Map<String,Object> result = this.operatorService.checkOperatorByUserNameAndPassWord(loginDTO);
        return  result;
    }

    @GetMapping("/phoneLoginSubmit")
    public void phoneLoginSubmit(HttpServletResponse response) throws Exception{

        LoginDTO loginDTO = new LoginDTO();
        loginDTO.setUserName("000");
        loginDTO.setPassword("000");
        Map<String,Object> result = this.operatorService.checkOperatorByUserNameAndPassWord(loginDTO);

        Boolean  isLogin = Boolean.parseBoolean(result.get("isLogin").toString());
        if(isLogin == true)
        {
            Long currentOperatorId = this.baseService.getCurrentLoginOperator().getEId();
            String userName = this.baseService.getCurrentLoginOperator().getName();
            response.sendRedirect("http://localhost:8091/#/main/"+currentOperatorId+"/"+URLEncoder.encode(userName));
        }
        else
        {

        }
    }

    @GetMapping("/test")
    public String test(HttpServletRequest request) throws JSONException{
        System.out.println("ddddd");
        JSONObject result = new JSONObject();
        result.put("state",true);
        result.put("result","");
        result.put("data","可以访问,用户当前组织为");

        return result.toString();
    }


    @PostMapping("/test2")
    public String test2( HttpServletRequest request ){
        Enumeration<String> headers = request.getHeaderNames();
        while(headers.hasMoreElements()){
            String name = headers.nextElement();
        }

        return "hello cloud";
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        this.operatorService.logout();
        return "OK";
    }
    @GetMapping("/constructNewTree")
    public ModuleTreeNode constructNewTree(@RequestParam  Long operatorId, @RequestParam String moduleType) {
        return this.moduleService.constructNewTree(operatorId, moduleType);
    }
}
